export default {
  data() {
    return {
      orgList: [],
    }
  },
  methods: {
    formatOrgListByCode(orgList, orgCode) {
      orgCode = orgCode || "999999999";
      const foundOrgList = orgList.filter(org => org['superOrgCode'] === orgCode);
      const result = [];
      foundOrgList.forEach(org => {
        result.push({
          label: org['orgName'],
          value: org['orgCode'],
          key: org['orgCode'],
          children: this.formatOrgListByCode(orgList, org['orgCode'])
        });
      });
      return result;
    },
    async fetchOrgList() {
      try {
        const res = await this.$get('/admin/market/cashierManager/getOrgList');
        if (res.status === 200) {
          console.log('获取机构树成功');
          this.orgList = this.formatOrgListByCode(res.data);
        }
      } catch (e) {
        this.$message.error('获取机构树失败');
      }

    }
  },
  created() {
    this.fetchOrgList();
  }
}
